export default {
	data() {
		return{
			// 通用SRC
			SRC: '',
			// 存放groupID
			groupId : '',
			// 存放组数据
			groups: [
			],
			group: '',
			// 存放用户数据
			users:[],
			user: [],
			renderFunc(h, option) {
				return <span>{ option.label }</span>;
			}
		} 
	},
	methods: {
		// 通用 成功 与 失败
		successFun(data) {
			this.$message({
				message: data,
				type: 'success'
			});
		},
		errorFun(data) {
			this.$message.error(data);
		},
		handleChange(value, direction, movedKeys) {
			// console.log(value, direction, movedKeys);
		},
		// 组切换
		change(val) {
			this.groupId = val;
			this.$http.post(`${this.SRC}monitor_user_group/users.do`,{groupId:val},{emulateJSON:true}).then(function(res){
				if(res.body['code']===0){
					var data = res.body['msg'];
					this.user = [];
					for(var i=0; i<data.length; i++){
						this.user.push(data[i][0]);
					}
				}else{
					this.errorFun(res.body['msg'])
				}
			})
		},
		// 提交
		save() {
			if(this.groupId === ""){
				this.errorFun("组别不能为空！")
				return;
			}
			this.$http.post(`${this.SRC}monitor_user_group/modify.do`,{groupId:this.groupId,userIdList:this.user},{emulateJSON:true}).then(function(res){
				if(res.body['code']===0){
					this.successFun(res.body['msg'])
				}else{
					this.errorFun(res.body['msg'])
				}
			})
		},
		// 获取组的数据 获取用户的数据
		getData() {
			var that = this;
			var message = function(url){
				return new Promise(function(resolve,reject){
					that.$http.get(that.SRC + url).then(function(res){
						if(res.body['code']===0){
							resolve(res.body['msg']);
						}else{
							reject(false);
						}
					})
				})
			};
			var urlData = ['monitorgroup/list.do','monitoruser/list.do'];
			function renderAll() {
				return Promise.all([message(urlData[0]), message(urlData[1])]);
			};
			renderAll().then(function(val){
				that.groups = val[0];
				that.users = val[1];
			})
		}
	},
	mounted() {
		this.SRC = this.$store.getters.src3;
		this.getData();
	}
} 